<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* Base Object Class
*
* All Objects in the Game Share these Characteristics by Definition
*/

/*
*  This is, inevitably, an extremely messy class because it's guts have to be general.
*  But its incredible usefulness in encapsulating most of the game's functionality
*  across all it's different types of objects is unrivaled.
*/

class BaseObject {
  public $id;
  public $name;
  public $short_descr;
  public $description;
  public static $top_id;
  public $instance_id;  	// This if you have ten copies of an object, this can tell them apart
  //public $vnum;  			// For Scripting References (in the future) // A Funky little thing of incredible importance... For identifying things
  							// Vnums are covered by flags
  
  public $thumb;			// The small image (specific to this server/client setup)
  public $image; 			// Potentially it has one (specific to this server/client setup)
  
  /* The Horror, the Horror, nontrivial data types in BaseObject. Ack. But necessary and true. */
  public $in_object;	  		// Where it is (BaseObject)
  public $was_in_object;		// Where it was (BaseObject)

  public $items;			// For use with containers, and people carrying stuff
 
  public $weight;			// You can't see it or touch it, but everybody has it. (And you can see it when you wear that dress).
 
  public $all_flags; 		// Any boolean value of any kind on the object (all options, settings, affects, etc.)
	
	// Affects are highly extensible, and can, when applied, inject executable code into an object class, that is checked
	// prior to any state change. When the affect is lifted, the code is deleted.  
  public $coded_affects;		// Array of affects with checkable code
  
  // Database Variables... have to have them...
  public $in_object_id;
  
  function __construct() {
  	  $this->instance_id = BaseObject::$top_id++;
	  $this->items			= array();
	  $this->affects 		= array();
	  $this->all_flags 		= array();
	  $this->coded_affects	= array();
	  
	  $this->thumb = "";
	  $this->image = "";
	  
		// Ease up on the database;
	  $this->id = DEF_NEG; // Cannot stay inthe database this way, 
	  // but can be generically referenced by others, for instance, by exits needing a place to point
	  $this->weight = DEF_NEG;
  }
  
  	function check_affects() {
		if(func_num_args()==2) { $arg1 = func_get_arg(0); $arg2 = func_get_arg(1); }
		if(func_num_args()==1) { $arg1 = func_get_arg(0); $arg2 = ""; }
		if(func_num_args()==0) { $arg1 = ""; $arg2 = ""; }
		
		// These arguments can be anything...
		foreach($this->coded_affects as $a) {
			if($a->run_code($this,$arg1,$arg2)) {
				return true; // We cannot execute this action under this affect
			}
		}
	}
	
	function in_room() {
		if(func_num_args()==1) { 
			// Grab it and stick it in the room
			$this->in_object = func_get_arg(0); 
			return; 
		}
		// If it's not in the room directly, the object above it is...
		if(get_class($this->in_object)=="Room") {
			return $this->in_object;
		} else {
			// Recursively search
			return $this->in_object->in_room();
		}
		// Should never throw, but might need a check for isset()
		// if you take things out of rooms and then check this
	}
	
	function was_in_room() {
		if(func_num_args()==1) { 
			// Grab it and Point to it's old room
			$this->was_in_object = func_get_arg(0); 
			return; 
		}
		// If it's not in a room directly, the object above it is...
		if(get_class($this->was_in_object)=="Room") {
			return $this->was_in_object;
		} else {
			// Recursively search
			return $this->was_in_object->was_in_room();
		}	
	}
  
  /* -- LOWEST LEVEL FUNCTIONS -- */
  
  // -- Database (Permanent Storage)
  function put() {
  	dbHandler::save($this);
  }
  
  function get($id) {
  	set_fields_equal($this,dbHandler::load($this,$id));
	rmv_slashes_object($this);
  }
  
  // This function allows us to be totally agnostic about user
  // flags of any kind whatsoever
  // -- Flags
  function is($flag) {
	return IS_SET_EXTENDED($this->all_flags,$flag);
  }
  
  function has($flag) {
	return IS_SET_EXTENDED($this->all_flags,$flag);
  }
  
  function set($var) {
	SET_EXTENDED_BIT($this->all_flags,$var);
	clientHandler::toggleFlag($this,$var);
  }
  
  function rmv($var) {
	RMV_EXTENDED_BIT($this->all_flags,$var);
	clientHandler::toggleFlag($this,$var);
  }
  
  // Assignment through this function guarantees that an object 
  // update goes directly to the (relevant) client
  // -- Triggers for Data to Client
  function change($objectName,$value) {
  	clientHandler::change($this,$objectName,$value);
  }
}

?>